#include "brightnessdialog.h"
#include "ui_brightnessdialog.h"

#include <QPainter>

BrightnessDialog::BrightnessDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::BrightnessDialog)
{
    ui->setupUi(this);
    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint & ~Qt::WindowMinimizeButtonHint);
    setWindowTitle(tr("Adjust the brightness and contrast"));
    setModal(true);
    setFixedSize(314, 148);

    connect(ui->brightnessSlider, SIGNAL(valueChanged(int)), ui->brightnessSpinBox, SLOT(setValue(int)));
    connect(ui->brightnessSpinBox, SIGNAL(valueChanged(int)), this, SLOT(emitBrightnessChanged(int)));
    connect(ui->contrastSlider, SIGNAL(valueChanged(int)), ui->contrastSpinBox, SLOT(setValue(int)));
    connect(ui->contrastSpinBox, SIGNAL(valueChanged(int)), this, SLOT(emitContrastChanged(int)));
    connect(ui->applyButton, SIGNAL(clicked()), this, SLOT(applyButtonClicked()));
    connect(ui->cancelButton, SIGNAL(clicked()), this, SLOT(cancelButtonClicked()));
}

BrightnessDialog::~BrightnessDialog()
{
    delete ui;
}

void BrightnessDialog::closeEvent(QCloseEvent *)
{
    emit cancelOperator();
}

void BrightnessDialog::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setPen(Qt::gray);
    painter.drawRect(10, 10, width() - 20, height() - 20);
}

void BrightnessDialog::showEvent(QShowEvent *)
{
    ui->brightnessSlider->setValue(0);
    ui->contrastSlider->setValue(100);
}

void BrightnessDialog::emitBrightnessChanged(int brightness)
{
    ui->brightnessSlider->setValue(brightness);
    emit brightnessChanged(brightness);
}

void BrightnessDialog::emitContrastChanged(int contrast)
{
    ui->contrastSlider->setValue(contrast);
    emit contrastChanged(contrast);
}

void BrightnessDialog::applyButtonClicked()
{
    QString str = tr("Adjust the brightness and contrast. ")
            + tr("Brightness : ") + QString::number(ui->brightnessSlider->value())
            + tr(", Contrast : ") + QString::number(ui->contrastSlider->value()) + tr(".");
    emit applyOperator(str);
    close();
}

void BrightnessDialog::cancelButtonClicked()
{
    close();
}
